package windows;

import gameObject.Arrow;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;

import main.Core;
import action.Transiction;
import constants.ImagesConstants;
import constants.SoundConstants;
import control.WavPlayer;
import data.ImageSprite;
import data.TextSprite;

public class LoadingWindow extends SceneWindow {

	private Arrow arrow;
	private Transiction transiction;
	private float transictionFactor;
	private ImageSprite[] fileWindow;
	
	private static final int NUM_ARQUIVOS = 3;

	public LoadingWindow(Core c) {
		super(c);
	}

	@Override
	public void Start() {
		try {
			ImageSprite backGround = new ImageSprite(ImageIO.read(new File(ImagesConstants.OPENING)), 0, 0, 800, 600, getFather());
			ImageSprite textWindow = new ImageSprite(ImageIO.read(new File(ImagesConstants.WINDOW_BACK_BORDER)), 20, 20, 760, 128, getFather());
			ImageSprite[] books = new ImageSprite[NUM_ARQUIVOS];
			fileWindow = new ImageSprite[NUM_ARQUIVOS];

			TextSprite loadingText = new TextSprite("Carregar qual arquivo?", 120, 90, getFather());
			TextSprite[] fileText = new TextSprite[NUM_ARQUIVOS];

			arrow = new Arrow(100, 225, 20, 20, getFather(), NUM_ARQUIVOS);
			arrow.setAmountToMove(147);

			this.add("backGround", backGround);
			this.add("textWindow", textWindow);
			this.add("loadingText", loadingText);

			int yWindow = 168;
			int yText = 240;
			int yBook = 230;
			for (int i = 0; i < fileWindow.length; i++) {
				
				fileWindow[i] = new ImageSprite(ImageIO.read(new File(ImagesConstants.WINDOW_BACK_BORDER)), 20, yWindow, 760, 128, getFather());
				this.add("fileWindow" + i, fileWindow[i]);

				fileText[i] = new TextSprite("Arquivo Vazio.", 120, yText, getFather());
				this.add("fileText" + i, fileText[i]);

				books[i] = new ImageSprite(ImageIO.read(new File(ImagesConstants.BOOK)), 650, yBook, 20, 20, getFather());
				this.add("books" + i, books[i]);

				yWindow += 148;
				yText += 148;
				yBook += 148;
			}

			this.add("arrow", arrow);
			transictionFactor = 1;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void OnKeyPress(KeyEvent keyPressed) {
		{
			// do key related tasks here.
			if (confirmar(keyPressed.getKeyCode())) {
				try {
					WavPlayer.play(SoundConstants.CANCELAR);
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
			} else if (keyPressed.getKeyCode() == KeyEvent.VK_ESCAPE) {
				transiction = new Transiction(0f, 0.5f);
				setNextWindow(new MenuWindow(getFather()));
			} else {
				if (null == transiction){
					arrow.OnKeyPress(keyPressed);
				}
			}
		}
	}

	@Override
	public void Update(double elapsedTime) {
		super.Update(elapsedTime);
		if (null != transiction) {
			transictionFactor = (float) (transiction.update(1, elapsedTime));

			if (null != arrow) {
				arrow.setVisible(false);
				arrow = null;
			}
			if (transictionFactor <= 0) {
				Terminate(getNextWindow());
			}
		}
	}

	@Override
	public void Draw(Graphics2D g) {
		super.Draw(g);
		if (transictionFactor <= 1) {
			g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transictionFactor));
		}
	}
	
	@Override
	public void Terminate() {
		Terminate(null);
	}

	@Override
	public void Terminate(SceneWindow nextWindow) {
		getFather().changeScene(nextWindow);
	}

}
